//<![CDATA[

jQuery().ready(function() {
	system_redirect = false;
	system_logoutCancel = false;

	window.onbeforeunload = enableExistWarning;
	//enableExistWarning();
	disableExistWarningForHyperLink();
	//disableRightClick();
	initHeaderCtrl();
	//resotreMenuPreference();
	
	getFocus();
	
	//disableF5Key();
	
	versionLabel();
});

function versionLabel(){
	
	var t = $("title").text().toUpperCase();
	if ( t.indexOf('DEV', 0) != -1 ){
		createVersionLabel('DEV 開發版本');
	} else if (t.indexOf('UAT', 0) != -1) {
		createVersionLabel('UAT 測試版本');
	}
}

function createVersionLabel( content ){
	jQuery('#container').append("<div id=\"versionLabel\"></div>"); 
	jQuery("#versionLabel").css("z-index","1000");
	jQuery("#versionLabel").css("color","FF0000");
	jQuery("#versionLabel").css("font-size","48px");
	jQuery("#versionLabel").css("position","fixed");
	jQuery("#versionLabel").html(content);
	if (!system_showMenu){
		jQuery("#versionLabel").css("top",80);
	} else {
		jQuery("#versionLabel").css("top",160);
	}
	jQuery("#versionLabel").css("left",jQuery(window).width()-jQuery("#versionLabel").width()-20);
	jQuery("#versionLabel").mouseover(
		function(){
			jQuery("#versionLabel").fadeOut();		
			setTimeout(
				function(){
					jQuery("#versionLabel").fadeIn();
				}, 2000
			);
		}
	);
	jQuery(window).resize(
		function (){
			jQuery("#versionLabel").css("top",80);
			jQuery("#versionLabel").css("left",jQuery(window).width()-jQuery("#versionLabel").width()-20);						
		}
	);
}

function enableExistWarning( e1 ) {
	var e = e1 || window.event;
	if ( !system_redirect ){
		e.returnValue = '請勿強行離開頁面.\n如要登出, 請按 登出 離開系統.\n\n警告: 強行離開頁面會引起資料鎖定.';
	}
}

//Exit warning
//function enableExistWarning() {
//	jQuery(window).bind('beforeunload', function(){ 
//		if ( !system_redirect ){
//			return '請勿強行離開頁面.\n如要登出, 請按 登出 離開系統.\n\n警告: 強行強行離開頁面會引起資料鎖定.';
//		}
//	});
//}

function confirmLogout(){
	if (confirm("是否登出系統? 未儲存的資料將會流失!")){
		allowRedirect();
		return true;
	} else {
		disallowRedirect();
		system_logoutCancel = true;
	}
	return false; 
}
/*
function resotreMenuPreference(){
	if (jQuery.cookie("system_showMenu") != "true" ) {
		hideHeader();
	} else { 
		showHeader();
	}
}
*/
function disableF5Key(){
	
	if(document.attachEvent){
		document.attachEvent("onkeydown", my_onkeydown_handler); 
	}else{
		document.addEventListener("keydown", my_onkeydown_handler, false);
	}
	function my_onkeydown_handler(){
		switch (event.keyCode){ 
			case 116 : // 'F5' 
				event.returnValue = false; 
				event.keyCode = 0; 
				alert("不支授 F5 重整頁面"); 
				break;
		} 
	}
}

//Set all hyperlink default allow redirect
function disableExistWarningForHyperLink(){
	jQuery("a:not([class~='localLink'])").each( function ( i, e ){
		jQuery(this).bind('click.autoAllow',function( event ){
			if ( !system_logoutCancel ){
				allowRedirect();
			} else {
				system_logoutCancel = false;
			}
		});
	});
}

function disallowRedirect(){
	system_redirect = false;
}

function allowRedirect(){
	system_redirect = true;
}

//Disable right click
function disableRightClick(){
    jQuery().bind("contextmenu", function(e) {
    	switch ( e.target.tagName ){
    		case "INPUT":
    			break;
    		default:
    			e.preventDefault();
    	}
    }); 
}

// make cursor on the first focusable input element
function getFocus(){
    var x = document.getElementsByTagName("input");
	for (var c = 0; c < x.length; c++) {
		if (x[c].type == 'text') {
			x[c].select();
			try {
				x[c].focus();
				break;
			} catch (err) {
			}
		}

		if (x[c].type == 'button') {
			try {
				if (x[c].isFocusable()) {
					try {
						x[c].focus();
						break;
					} catch (ierr) {
						
					}
				}
			} catch (err) {
				
			}
		}
	}
}



// get the query string parameter
function getQuerystring(key, default_) {
	if (default_==null) 
		default_=""; 
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
		return default_;
	else
		return qs[1];
}

function showHeader() {
	jQuery('#mainHeader').css("display", "block");
	jQuery("#btnShowMainHeader").attr("src", jQuery("#btnShowMainHeader").attr("src").replace("arrow_down.png", "arrow_up.png"));
	jQuery('#content').css('margin', '160px 0 25px 0');
	system_showMenu = true;
}

function hideHeader() {
	jQuery('#mainHeader').css("display", "none");
	jQuery("#btnShowMainHeader").attr("src", jQuery("#btnShowMainHeader").attr("src").replace("arrow_up.png", "arrow_down.png"));
	jQuery('#content').css('margin', '80px 0 25px 0');
	system_showMenu = false;
}

function slideDownHeader() {
	jQuery("#btnShowMainHeader").attr("src", jQuery("#btnShowMainHeader").attr("src").replace("arrow_down.png", "arrow_up.png"));
	jQuery('#mainHeader').slideDown('slow'); 
	jQuery('#content').css('margin', '160px 0 25px 0');
	system_showMenu = true;	
	jQuery("#versionLabel").css("top",160);	
}

function slideUpHeader() {
	jQuery("#btnShowMainHeader").attr("src", jQuery("#btnShowMainHeader").attr("src").replace("arrow_up.png", "arrow_down.png"));
	jQuery('#mainHeader').slideUp('slow'); 
	jQuery('#content').css('margin', '80px 0 25px 0');
	system_showMenu = false;
	jQuery("#versionLabel").css("top",80);
}

function initHeaderCtrl() {
	system_showMenu = false;
	jQuery("#showMainHeader").click(
		function (event) {
			if (!system_showMenu) {
				slideDownHeader();
			} else {
				slideUpHeader();
			}
			//jQuery().cookie("system_showMenu", system_showMenu + "");
			event.stopPropagation();
		}
	);
}

function inputUCase( element ){
	try {
		jQuery('input[id="' + element.id + '"]').val(jQuery.trim((jQuery('input[id="' + element.id + '"]').val() + "").toUpperCase()));
	} catch ( err ){
		// do nothing;
	}
}

// sonia.wong.c.y 20110914 CNQA(FG) start
function enterToSubmit(event){
	if ( event.keyCode == 13 ){
		jQuery("input[name$=viewData]").click();
	}
}

function disableInputField(element){
	try {
		jQuery('input[id="' + element.id + '"]').attr('disabled', true);
	} catch (err) {
		// do nothing;
	}
}

function enableInputField(element){
	try {
		jQuery('input[id="' + element.id + '"]').attr('disabled', false);
	} catch (err) {
		// do nothing;
	}
}
// sonia.wong.c.y 20110914 CNQA(FG) end

// Added By Daniel 20120706 
function getPrinterFullNameByPrefix(printerListStr, printerName){
	var printerList = printerListStr.split("|");
	for(var i = 0; i < printerList.length; i++){
		if (printerList[i].toUpperCase() == printerName.toUpperCase()){
			jQuery.cookie(printerName, printerList[i]);
			return printerList[i];
		}	
	}
	for(var i = 0; i < printerList.length; i++){
		if (printerList[i].toUpperCase().indexOf(printerName.toUpperCase()) != -1){
			jQuery.cookie(printerName, printerList[i]);
			return printerList[i];
		}	
	}
	return null;
}
function isPrinterExistInList(printerListStr, printerName){
	var printerList = printerListStr.split("|");
	for(var i = 0; i < printerList.length; i++){
		if (printerList[i].toUpperCase() == printerName.toUpperCase()){
			jQuery.cookie(printerName, printerList[i]);
			return true;
		}	
	}
	return false;
}
// End Added By Daniel 20120706

function convertToUpperCase(obj){
	
	return obj.value = obj.value.toUpperCase();
}

function dldWithUrl(url){
	window.open(url);
}

function closemyself() { 
	window.opener=self; window.close(); 
} 

function setObjFocus(obj){
	obj.focus();
	obj.select();
}

function search_customer(flag,msg) {			
	
	if(flag) {
		if(window.confirm(msg)) {
			return true;					
		} else {
			return false;
		}	
	}
	return true;
}

//]]>